package com.efast.dmom.mgmt.service.les.impl;

import java.util.Date;
import java.util.List;

import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.dao.les.*;
import com.efast.dmom.mgmt.entity.les.TrBasPartStorage;
import com.efast.dmom.mgmt.entity.les.TwInbTaskDetail;
import com.efast.dmom.mgmt.entity.les.TwInbTaskHeader;
import com.efast.dmom.mgmt.entity.les.TwInboundAllocDetail;
import com.efast.dmom.mgmt.entity.les.TwInvSkuLoctl;
import com.efast.dmom.mgmt.service.les.TwInboundAllocDetailService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author chlp
 */
@Service
@Transactional
public class TwInboundAllocDetailServiceImpl implements TwInboundAllocDetailService {

    /**
     * 分配记录 TW_INBOUND_ALLOC_DETAIL
     */
    private final TwInboundAllocDetailDao twInboundAllocDetailDao;

    /**
     * 库存表 TW_INV_SKU_LOC_T_L
     */
    private final TwInvSkuLoctlDao twInvSkuLoctlDao;

    /**
     * 上架任务表 TW_INB_TASK_HEADER
     */
    private final TwInbTaskHeaderDao twInbTaskHeaderDao;

    /**
     * 上架任务表 TW_INB_TASK_DETIAIL
     */
    private final TwInbTaskDetailDao twInbTaskDetailDao;

    /**
     * 零件仓储关系以及动态库位入库选择规则 tr_bas_part_storage
     */
    private final TrBasPartStorageDao trBasPartStorageDao;

    /**
     * 通用
     */
    private final CommonServiceDao commonServiceDao;

    /**
     * jdbc
     */
    @Qualifier("jdbcDao")
    @Autowired
    private JdbcDao jdbcDao;

    @Autowired
    public TwInboundAllocDetailServiceImpl(TwInboundAllocDetailDao twInboundAllocDetailDao, TwInvSkuLoctlDao twInvSkuLoctlDao, TwInbTaskHeaderDao twInbTaskHeaderDao, TwInbTaskDetailDao twInbTaskDetailDao, TrBasPartStorageDao trBasPartStorageDao,CommonServiceDao commonServiceDao) {
        this.twInboundAllocDetailDao = twInboundAllocDetailDao;
        this.twInvSkuLoctlDao = twInvSkuLoctlDao;
        this.twInbTaskHeaderDao = twInbTaskHeaderDao;
        this.twInbTaskDetailDao = twInbTaskDetailDao;
        this.trBasPartStorageDao = trBasPartStorageDao;
        this.commonServiceDao = commonServiceDao;
    }


    /**
     *
     * 功能描述:分配上架库位
     *
     * @param:
     * @return:
     * @auther: ChenLiPeng
     * @date: 18-5-14 下午6:26
     */
    @Override
    public void updateNonEmptyTwInboundAllocDetailById(TwInboundAllocDetail enti) {

        // 【1】：更新TW_INBOUND_ALLOC_DETAIL，将待入库的库位更新上去。
        // 状态（已分配0，已生成上架任务1，已完成上架2，部分上架3，无法分配4，关闭5，冲销6）
        enti.setItemStatus("0");
        twInboundAllocDetailDao.updateNonEmptyTwInboundAllocDetailById(enti);

        // 【2】：查询stage库位(此数据是确认收货的时候插入的)，对应的 可用数量-AVAILABLE_QTY和 库存数量-CUR_QTY
        List<TwInvSkuLoctl> listTwInvSkuLoctl = twInvSkuLoctlDao.selectTwInvSkuLoctl(
                new Assist(Assist.andEq("tw_inv_sku_loc_t_l.tm_bas_storage_id", enti.getStageBasStorageId()),
                        Assist.andEq("tw_inv_sku_loc_t_l.part_no_version", enti.getPartNoVersion()),
                        Assist.andEq("tw_inv_sku_loc_t_l.inb_serial_no", enti.getInbSerialNo())));

        // 【3】：向库存表（TW_INV_SKU_LOC_T_L）插入一条记录。库位是页面上手动分配的库位。
        TwInvSkuLoctl twInvSkuLoctl = new TwInvSkuLoctl();
        BeanUtils.copyProperties(enti, twInvSkuLoctl);
        // 箱数
        twInvSkuLoctl.setPackageNum(1);
        // 可用数量-AVAILABLE_QTY
        twInvSkuLoctl.setAvailableQty(0);
        // 库存数量-CUR_QTY
        twInvSkuLoctl.setCurQty(0);
        List<TrBasPartStorage> listTrBasPartStorage = trBasPartStorageDao
                .selectTrBasPartStorage(new Assist(Assist.andEq("part_no_version", enti.getPartNoVersion()),
                        Assist.andEq("loc_id", enti.getPreTmBasStorageId())));
        if (listTrBasPartStorage.size() == 0) {
            throw new ServiceException("你选择的库位在零件仓库关系中没有定义");
        }
        TwInvSkuLoctl twinvskuloctl = commonServiceDao.queryWhDlocLoc(enti.getPreTmBasStorageId());
        twInvSkuLoctl.setLocNo(twinvskuloctl.getLocNo());
        twInvSkuLoctl.setDlocNo(twinvskuloctl.getDlocNo());
        twInvSkuLoctl.setWarehouseNo(twinvskuloctl.getWarehouseNo());
        // 待上架仓储单元ID
        twInvSkuLoctl.setTmBasStorageId(enti.getPreTmBasStorageId());
        // 待入库数量-R_INB_QTY == 待上架数量
        twInvSkuLoctl.setrInbQty(listTwInvSkuLoctl.get(0).getAvailableQty());
        // ASN待入库上架数量-ASN_R_INB_QTY
        twInvSkuLoctl.setAsnrInbQty(listTwInvSkuLoctl.get(0).getAvailableQty());
        twInvSkuLoctlDao.insertNonEmptyTwInvSkuLoctl(twInvSkuLoctl);

        // 【4】：分配完成生成上架任务清单--TW_INB_TASK_HEADER
        TwInbTaskHeader twInbTaskHeader = new TwInbTaskHeader();
        twInbTaskHeader.setCompanyCode(SpringWebUtils.getRequestCompany());
        twInbTaskHeader.setCreateTime(new Date());
        twInbTaskHeader.setCreateUser(SpringWebUtils.getRequestUser());
        twInbTaskHeader.setMarkStatus(1);
        twInbTaskHeader.setTaskCreatetime(new Date());
        twInbTaskHeader.setTaskCreateman(SpringWebUtils.getRequestUser());
        twInbTaskHeader.setAsnSheetNo(enti.getAsnSheetNo());
        String inboundAllocNo = jdbcDao.executeProcGetCode("tw_inb_task_header", "efast");
        twInbTaskHeader.setInboundAllocNo(inboundAllocNo);
        // 0：新任务 1：任务完成 2：任务部分完成
        twInbTaskHeader.setInboundStatus("0");
        // 0:未打印 1：已打印
        twInbTaskHeader.setPrintStatus("0");
        twInbTaskHeaderDao.insertNonEmptyTwInbTaskHeader(twInbTaskHeader);

        // 【5】：分配完成生成上架任务清单 明细表--TW_INB_TASK_DETIAIL
        TwInbTaskDetail twInbTaskDetail = new TwInbTaskDetail();
        BeanUtils.copyProperties(enti, twInbTaskDetail);
        twInbTaskDetail.setPartNamec(enti.getPartNamec());
        // 待上架数量
        twInbTaskDetail.setPreNboundQty(enti.getPreNboundQty());
        // inbound_pack_qty=入库包装容量
        // double preInbPackNum=
        // listTwInvSkuLoctl.get(0).getAvailableQty()/listTwInvSkuLoctl.get(0).getInboundPackQty();
        // 待上架箱数
        twInbTaskDetail.setPreInbPackNum(1);
        // 上架状态（新任务0，任务完成1,部分上架2,关闭3）
        twInbTaskDetail.setInboundStatus("0");
        // 上架任务清单打印状态（0打印1已打印）
        twInbTaskDetail.setPrintStatus("0");
        // 入库标签打印状态（0打印1已打印）
        twInbTaskDetail.setSerialPrintStatus("0");
        // 任务标签打印状态（0打印1已打印）
        twInbTaskDetail.setTaskPrintStatus("0");
        twInbTaskDetail.setTwInbTaskHeaderId(twInbTaskHeader.getTwInbTaskHeaderId());
        twInbTaskDetail.setTwInboundAllocDetailId(enti.getTwInboundAllocDetailId());
        twInbTaskDetail.setLineNo(1);
        twInbTaskDetail.setInboundAllocNo(inboundAllocNo);
        twInbTaskDetail.setCompanyCode(SpringWebUtils.getRequestCompany());
        twInbTaskDetail.setCreateTime(new Date());
        twInbTaskDetail.setCreateUser(SpringWebUtils.getRequestUser());
        twInbTaskDetail.setMarkStatus(1);
        twInbTaskDetailDao.insertNonEmptyTwInbTaskDetail(twInbTaskDetail);
    }

    @Override
    public long getTwInboundAllocDetailRowCount(Assist assist) {
        return twInboundAllocDetailDao.getTwInboundAllocDetailRowCount(assist);
    }

    @Override
    public List<TwInboundAllocDetail> selectTwInboundAllocDetail(Assist assist) {
        return twInboundAllocDetailDao.selectTwInboundAllocDetail(assist);
    }

    @Override
    public TwInboundAllocDetail selectTwInboundAllocDetailByObj(TwInboundAllocDetail obj) {
        return twInboundAllocDetailDao.selectTwInboundAllocDetailByObj(obj);
    }

    @Override
    public TwInboundAllocDetail selectTwInboundAllocDetailById(Integer id) {
        return twInboundAllocDetailDao.selectTwInboundAllocDetailById(id);
    }

    @Override
    public int insertTwInboundAllocDetail(TwInboundAllocDetail value) {
        return twInboundAllocDetailDao.insertTwInboundAllocDetail(value);
    }

    @Override
    public int insertNonEmptyTwInboundAllocDetail(TwInboundAllocDetail value) {
        return twInboundAllocDetailDao.insertNonEmptyTwInboundAllocDetail(value);
    }

    @Override
    public int insertTwInboundAllocDetailByBatch(List<TwInboundAllocDetail> value) {
        return twInboundAllocDetailDao.insertTwInboundAllocDetailByBatch(value);
    }

    @Override
    public int deleteTwInboundAllocDetailById(Integer id) {
        return twInboundAllocDetailDao.deleteTwInboundAllocDetailById(id);
    }

    @Override
    public int deleteTwInboundAllocDetail(Assist assist) {
        return twInboundAllocDetailDao.deleteTwInboundAllocDetail(assist);
    }

    @Override
    public int updateTwInboundAllocDetailById(TwInboundAllocDetail enti) {
        return twInboundAllocDetailDao.updateTwInboundAllocDetailById(enti);
    }

    @Override
    public int updateTwInboundAllocDetail(TwInboundAllocDetail value, Assist assist) {
        return twInboundAllocDetailDao.updateTwInboundAllocDetail(value, assist);
    }

    @Override
    public int updateNonEmptyTwInboundAllocDetail(TwInboundAllocDetail value, Assist assist) {
        return twInboundAllocDetailDao.updateNonEmptyTwInboundAllocDetail(value, assist);
    }

}